<?php

/**
 * 简单商品价格计算类(注解版)
 * price::price_uid(商品ID); 获取商品价格用
 * price::price_uid(商品ID,用户ID); 获取商品价格用(自定义用户ID)
 * price::reckon(-2,商品ID,1); 获取商品详细套餐价格用,返回数组
 */

/**
 * Class price
 */
class price
{
    /**
     * @param int $grade 用户等级
     * @param int $profits 商品利润
     * @param $money 商品成本
     * @param $type 是否是游客？1非游客
     * @return array
     * 根据商品ID取出需要的价格
     */
    public static function price_uid($grade = 1, $profits = 100, $money, $custom = '', $rise = 0)
    {
        global $conf;
        $rise = (float)$rise;
        if ($rise < 0) $rise = 0;
        if ($grade == -1) {
            $arr = self::level_do((float)$money, (float)$profits, 1, $custom, $rise);
            $arr['level'] = '游客售价';
        } else {
            $arr = self::level_do((float)$money, (float)$profits, ((int)$grade <= 1 ? 1 : (int)$grade), $custom, $rise);
        }

        if ($conf['LevelDisplay'] == -1) {
            $arr['level_arr'] = [];
        } else if (count($arr['level_arr']) >= 1) {
            $Data = [];
            foreach ($arr['level_arr'] as $value) {
                $Data[] = $value;
            }
            $arr['level_arr'] = $Data;
        }

        return $arr;
    }

    /**
     * @param $cost  商品成本
     * @param $profits 商品利润比例
     * @param $grade 用户等级
     * 进行用户等级计算
     */
    public static function level_do($cost, $profits, $grade, $custom = '', $rise = 0)
    {
        global $mid_arr;
        if (count($mid_arr) == 0) {
            return [
                'price' => $cost * (1.3 * ($profits == 0 ? 0 : ($profits / 100))) + ($cost * (1.3 * ($profits == 0 ? 0 : ($profits / 100))) * ($rise / 100)),
                'points' => $cost * (1300 * ($profits == 0 ? 1 : ($profits / 100))) + ($cost * (1300 * ($profits == 0 ? 1 : ($profits / 100))) * ($rise / 100)),
                'level' => '正常用户',
                'level_arr' => [],
            ];
        } else {
            $arr = self::CalculationLevel($mid_arr, $grade - 1, $cost, $profits, $custom, $rise);
        }
        return $arr;
    }

    /**
     * @param $mid_arr 用户等级列表
     * @param $grade 用户等级
     */
    public static function CalculationLevel($mid_arr, $grade = 0, $cost, $profits = 100, $custom = '', $rise = 0)
    {
        if ($custom <> '' && $custom <> null) {
            $Custom_arr = explode(',', $custom);
        } else $Custom_arr = [];
        $a = 0;
        if (count($mid_arr) == 1) {
            $mid_arr['priceis'] = $mid_arr['priceis'] * ($profits == 0 ? 0 : ($profits / 100));
            $mid_arr['pointsis'] = $mid_arr['pointsis'] * ($profits == 0 ? 1 : ($profits / 100));
            if (isset($Custom_arr[$a])) {
                if (strstr($Custom_arr[$a], '|')) {
                    $Sro = explode('|', $Custom_arr[$a]);
                    return [
                        'price' => round(($Sro[0] - 0) + (($Sro[0] - 0) * ($rise / 100)), 8),
                        'points' => round(($Sro[1] - 0) + (($Sro[1] - 0) * ($rise / 100)), 0),
                        'level' => $mid_arr['name'],
                        'level_arr' => [],
                    ];
                } else {
                    return [
                        'price' => round(($Custom_arr[$a] - 0) + ((($Custom_arr[$a] - 0)) * ($rise / 100)), 8),
                        'points' => round($cost * $mid_arr['pointsis'] + (($cost * $mid_arr['pointsis']) * ($rise / 100)), 0),
                        'level' => $mid_arr['name'],
                        'level_arr' => [],
                    ];
                }
            }
            return [
                'price' => round(($cost + (($mid_arr['priceis'] / 100) * $cost)) + (($cost + (($mid_arr['priceis'] / 100) * $cost)) * ($rise / 100)), 8),
                'points' => round($cost * $mid_arr['pointsis'] + (($cost * $mid_arr['pointsis']) * ($rise / 100)), 0),
                'level' => $mid_arr['name'],
                'level_arr' => [],
            ];
        } else { //几个等级
            $data = [];
            $app = 0;
            foreach (array_reverse($mid_arr) as $v) {

                $v['priceis'] = $v['priceis'] * ($profits == 0 ? 0 : ($profits / 100));
                $v['pointsis'] = $v['pointsis'] * ($profits == 0 ? 1 : ($profits / 100));
                if ($app == 0) $v['name'] = $v['name'] . '/游客';

                if (isset($Custom_arr[$a])) {
                    if (strstr($Custom_arr[$a], '|')) {
                        $Sro = explode('|', $Custom_arr[$a]);
                        $data[] = [
                            'name' => $v['name'],
                            'price' => round(($Sro[0] - 0) + (($Sro[0] - 0) * ($rise / 100)), 8), //售价
                            'points' => round(($Sro[1] - 0) + (($Sro[1] - 0) * ($rise / 100)), 0), //兑换价格
                        ];
                        unset($Sro);
                    } else {
                        $data[] = [
                            'name' => $v['name'],
                            'price' => round(($Custom_arr[$a] - 0) + (($Custom_arr[$a] - 0) * ($rise / 100)), 8), //售价
                            'points' => round($cost * $v['pointsis'] + (($cost * $v['pointsis']) * ($rise / 100)), 0), //兑换价格
                        ];
                    }
                } else {
                    $data[] = [
                        'name' => $v['name'],
                        'price' => round(($cost + (($v['priceis'] / 100) * $cost)) + (($cost + (($v['priceis'] / 100) * $cost)) * ($rise / 100)), 8), //售价
                        'points' => round($cost * $v['pointsis'] + (($cost * $v['pointsis']) * ($rise / 100)), 0), //兑换价格
                    ];
                }
                ++$app;
                ++$a;
            }
            $data_2 = $data;
            foreach ($data as $k => $v) {
                if ($k < $grade) unset($data[$k]);
            }
            if (count($mid_arr) <= $grade) {
                return [
                    'price' => $data_2[count($data_2) - 1]['price'],
                    'points' => $data_2[count($data_2) - 1]['points'],
                    'level' => $data_2[count($data_2) - 1]['name'],
                    'level_arr' => [],
                ];
            }

            return [
                'price' => $data[$grade]['price'],
                'points' => $data[$grade]['points'],
                'level' => $data[$grade]['name'],
                'level_arr' => $data,
            ];
        }
    }

    /**
     * @param int $cost 商品成本
     * @param $level 用户等级规则
     * @param $profits 商品利润比例
     * 计算站长后台售价列表
     */
    public static function price_list($cost = 0, $level, $profits = 100, $custom = '', $Goods)
    {
        $data = []; //初始化

        if ($Goods['specification'] == 1) {
            if ($custom <> '' && $custom <> null) {
                $Custom_arr = explode(',', $custom);
            } else $Custom_arr = [];
            $a = 0;
            foreach (array_reverse($level) as $v) {
                $v['priceis'] = $v['priceis'] * ($profits == 0 ? 0 : ($profits / 100));
                $v['pointsis'] = $v['pointsis'] * ($profits == 0 ? 1 : ($profits / 100));
                if (isset($Custom_arr[$a])) {
                    if (strstr($Custom_arr[$a], '|')) {
                        $Sro = explode('|', $Custom_arr[$a]);
                        $data[] = [
                            'name' => $v['name'],
                            'price' => round(($Sro[0] - 0), 4), //售价
                            'points' => round(($Sro[1] - 0), 0), //兑换价格
                            'cost' => $cost, //成本
                            'profits' => round((($Sro[0] - 0) - $cost), 4), //利润
                        ];
                    } else {
                        $data[] = [
                            'name' => $v['name'],
                            'price' => round(($Custom_arr[$a] - 0), 4), //售价
                            'points' => round($cost * $v['pointsis'], 0), //兑换价格
                            'cost' => $cost, //成本
                            'profits' => round(($Custom_arr[$a] - $cost), 4), //利润
                        ];
                    }
                } else {
                    $data[] = [
                        'name' => $v['name'],
                        'price' => round(($cost + (($v['priceis'] / 100) * $cost)), 2), //售价
                        'points' => round($cost * $v['pointsis'], 0), //兑换价格
                        'cost' => $cost, //成本
                        'profits' => round((($cost + (($v['priceis'] / 100) * $cost)) - $cost), 2), //利润
                    ];
                }
                ++$a;
            }
        } else {
            $SpRule = RlueAnalysis($Goods['specification_set'], $Goods, 1, false, -1);
            $SkuData = [
                'data' => $SpRule,
                'SKU' => json_decode($Goods['specification_sku'], TRUE),
            ];
            foreach (array_reverse($SkuData['data']['Parameter']) as $v) {
                $v['name'] = '规格模式';
                $data[] = $v;
            } 
        }

        return $data;
    }

    /**
     * @param $freight //运费模板数据
     * @param $input //下单信息
     * @param $price //商品最终售价
     * @param $num //购买数量
     * 计算运费模板，得出售价！
     */

    public static function freight($freight, $input, $price, $num)
    {
        if (($price * $num) >= (float)$freight['threshold']) {
            //减免运费
            return $price * $num;
        }

        $freight_arr = explode('|', $freight['region']);

        $retos = '';
        foreach ($freight_arr as $v) {
            $rets = explode(',', $v);
            if (strpos(json_encode($input, JSON_UNESCAPED_UNICODE), $rets[0]) !== false) {
                $retos = $rets;
            }
        }

        if ($retos <> '') { //运费已自定义地区
            $_SESSION['retos'] = $retos[0] . '运费'; //地区
            $freight['money'] = (float)$retos[1]; //运费
            $freight['exceed'] = (float)$retos[2]; //加价金额！
            $_SESSION['exceed'] = $retos[0] . '地区，份数超出' . $freight['nums'] . '份，则每份需额外加' . round($freight['exceed'], 2) . '元运费';
        } else {
            $_SESSION['retos'] = '通用运费 ';
            $_SESSION['exceed'] = '若购买份数超出' . $freight['nums'] . '份，则每份需额外加' . round($freight['exceed'], 2) . '元运费';
        }
        if ($num > $freight['nums']) { //购买数量超出，加运费
            return round((((float)$price * $num) + (float)$freight['money']) + ((float)$freight['exceed'] * (float)((float)$num - (float)$freight['nums'])), 2);
        } else { //加正常运费
            return round((((float)$price * $num) + (float)$freight['money']), 2);
        }
    }
}
